package finassist.controller;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionContext;
import finassist.model.Entiti;
import finassist.model.EntitiType;
import finassist.model.User;
import finassist.DAO.LoginUserManagerDAO;
/**
 * AJ Projects
 * LoginUserManager.java
 * Purpose: Login User Database.
 *
 * @author Karthick Natarajan
 * @version 1.0 9/10/13
 */

public class LoginUserManager {

	public int check(User user) {
		
		
		LoginUserManagerDAO logObj = new LoginUserManagerDAO();
		List<User> users = null;
		User user1 = null;
		int result = 0;
		try {
			
			users = logObj.getUserDetails(user);
			if(users.size()!=0)
			{
				user1 = users.get(0);
				ActionContext.getContext().getSession().put("sesUser", user1);
			}
			
			if(users.size()==0)
			{}
			else if(user1.getUsername().equalsIgnoreCase(user.getUsername()) && user1.getPassword().equalsIgnoreCase(user.getPassword())){
				if(user1.getRole().equalsIgnoreCase("it_user"))
				result = 10;
				if(user1.getRole().equalsIgnoreCase("super_user"))
					result = 11;
			    
			}
			else
				result = 0;
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	
public List<Entiti> list() {
		
	
	LoginUserManagerDAO logObj = new LoginUserManagerDAO();
		List<Entiti> entitis = null;
		try {
			
		entitis = logObj.getEntityDetails();	
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return entitis;
	}
	

public List<String> listEntitiTypes() {
		
	
	LoginUserManagerDAO logObj = new LoginUserManagerDAO();
		List<EntitiType> entitis = new ArrayList<EntitiType>();
		List<String> entitiList = new ArrayList<String>();
		try {
			
		entitis = logObj.getEntityTypes();	
		
		for (int i = 0; i < entitis.size(); i++) {
			entitiList.add(entitis.get(i).getName().toString());
		}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return entitiList;
	}
}
